﻿using System;
using System.Windows.Forms;

namespace Z.IconLibraryGenerator.WinForm
{
    public class MenuBar
    {
        #region Desginer

        /// <summary>The close project link.</summary>
        public HtmlElement uiCloseProjectLink;

        /// <summary>The exit link.</summary>
        public HtmlElement uiExitLink;

        /// <summary>The new project link.</summary>
        public HtmlElement uiNewProjectLink;

        /// <summary>The open link.</summary>
        public HtmlElement uiOpenLink;

        /// <summary>The save as link.</summary>
        public HtmlElement uiSaveAsLink;

        /// <summary>The save link.</summary>
        public HtmlElement uiSaveLink;

        /// <summary>The web browser control.</summary>
        public WebBrowser uiWebBrowser;

        #endregion

        /// <summary>Constructor.</summary>
        /// <param name="webBrowser">The web browser control.</param>
        public MenuBar(WebBrowser webBrowser)
        {
            try
            {
                uiWebBrowser = webBrowser;

                if (uiWebBrowser.Document == null)
                {
                    throw new Exception("Document cannot be null.");
                }

                uiNewProjectLink = uiWebBrowser.Document.GetElementById("uiNewProjectLink");
                uiOpenLink = uiWebBrowser.Document.GetElementById("uiOpenLink");
                uiSaveLink = uiWebBrowser.Document.GetElementById("uiSaveLink");
                uiSaveAsLink = uiWebBrowser.Document.GetElementById("uiSaveAsLink");
                uiCloseProjectLink = uiWebBrowser.Document.GetElementById("uiCloseProjectLink");
                uiExitLink = uiWebBrowser.Document.GetElementById("uiExitLink");

                BindEvents();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"General Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>Bind events.</summary>
        public void BindEvents()
        {
            uiNewProjectLink.Click += (sender, args) =>
            {
                My.CurrentProject = new ProjectConfiguration();
                uiWebBrowser.Url = new Uri(My.Url.ProjectSettings);
            };
            uiOpenLink.Click += (sender, args) => ProjectOpen_OnClick(null, null);
            uiExitLink.Click += (sender, args) => Application.Exit();
        }


        /// <summary>Event handler. Called by ProjectOpen for on click events.</summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">HTML element event information.</param>
        public void ProjectOpen_OnClick(object sender, HtmlElementEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                Filter = @"Icon Generator Project File (*.zicongen)|*.zicongen|All files (*.*)|*.*",
                InitialDirectory = @"C:\",
                Title = @"Open a Icon Library Generator Project File"
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string path = dialog.FileName;
                OpenProject(path);
            }
        }

        /// <summary>Opens a project.</summary>
        /// <param name="projectPath">Full pathname of the project file.</param>
        /// <returns>true if it succeeds, false if it fails.</returns>
        public bool OpenProject(string projectPath)
        {
            string path = projectPath;

            ProjectConfiguration projectConfiguration = ProjectConfiguration.Load(path);

            if (projectConfiguration == null)
            {
                MessageBox.Show(@"Could not load the IconLibrary Generator project file. The file may be corrupted with invalid data.", @"Could not open file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                My.CurrentProject = projectConfiguration;
                uiWebBrowser.Url = new Uri(My.Url.ProjectSettings);
                return true;
            }

            return false;
        }
    }
}